# -*- Mode: shell-script -*-
#
# Copyright (C) 2013-2016 DNAnexus, Inc.
#
# This file is part of dx-toolkit (DNAnexus platform client libraries).
#
#   Licensed under the Apache License, Version 2.0 (the "License"); you may not
#   use this file except in compliance with the License. You may obtain a copy
#   of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#   License for the specific language governing permissions and limitations
#   under the License.
#
#
# Source this file in a bash shell to initialize DNAnexus environment
# variables:
# $ source environment

# This line is compatible with both bash and csh/tcsh. If running in csh, it sources the file "environment.csh" and quits.

test ! -z "$version" && echo "$shell" | grep -q csh && set SOURCE=`echo $_ | cut -f 2 -d " "` && set SOURCE=`dirname "$SOURCE"` && source "$SOURCE/environment.csh" && exit

# Resolve the location of this file
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done

# Get RHEL version
if [ -e /etc/redhat-release ] ; then
  RHEL_MAJOR_VERSION=$(grep -o "Red Hat Enterprise Linux .* release [0-9]\+" /etc/redhat-release | sed -e "s/Red Hat Enterprise Linux .* release //")
fi

export DNANEXUS_HOME="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

# Detect system installation of dx-toolkit
if [ "$DNANEXUS_HOME" == "/etc/profile.d" ]; then

  export DNANEXUS_HOME="/usr/share/dnanexus"
  # Private Python packages. We really ought not pollute PYTHONPATH with these though.
  export PYTHONPATH="/usr/share/dnanexus/lib/python2.7/site-packages:$PYTHONPATH"
  export CLASSPATH="/usr/share/java/dnanexus-api-0.1.0.jar:${CLASSPATH}"

else

  export PATH="${DNANEXUS_HOME}/bin:$PATH"
  export CLASSPATH="${DNANEXUS_HOME}/lib/java/*:${CLASSPATH}"

  if [ "$RHEL_MAJOR_VERSION" == "7" ]; then
    export PYTHONPATH="${DNANEXUS_HOME}/share/dnanexus/lib/python2.7/site-packages:${DNANEXUS_HOME}/lib64/python2.7/site-packages:$PYTHONPATH"
  else
    export PYTHONPATH="${DNANEXUS_HOME}/share/dnanexus/lib/python2.7/site-packages:${DNANEXUS_HOME}/lib/python:$PYTHONPATH"
  fi

fi

# Note: The default I/O stream encoding in Python 2.7 (as configured on ubuntu) is ascii, not UTF-8 or the system locale
# encoding. We reset it here to avoid having to set it for every I/O operation explicitly.
export PYTHONIOENCODING=UTF-8

eval "$(register-python-argcomplete dx|sed 's/-o default//')"

# Clean up old session files
(shopt -s nullglob;
 if [[ $HOME != "" ]]; then
     for session_dir in "${HOME}/.dnanexus_config/sessions/"*; do
         if ! ps -p $(basename "$session_dir") &> /dev/null; then
             rm -rf "$session_dir"
         fi
     done
 fi
)

if [[ -z "$DX_SECURITY_CONTEXT" && -n "$DX_AUTH_TOKEN" ]]; then
    export DX_SECURITY_CONTEXT="{\"auth_token_type\":\"Bearer\",\"auth_token\":\"$DX_AUTH_TOKEN\"}"
fi
